using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using System.Collections.Generic;
using Ext.Net;

public partial class admin_subscribers : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            StoreGroups.DataSource = NewsletterGroup.GetAllActiveGroups();
            StoreGroups.DataBind();
            LoadGroups();

            Session["_useSearch"] = null;
        }
    }

    protected void LoadGroups()
    {
        //ddlParent.Items.Clear();
        ReadWriteCustomCollection<NewsletterGroup> groups = NewsletterGroup.GetAllActiveGroups(); ;
        foreach (NewsletterGroup grp in groups )
        {
            //ddlParent.Items.Add(new System.Web.UI.WebControls.ListItem(productCategory.Key, productCategory.Value));
            ddlToolbarGroups.Items.Add(new Ext.Net.ListItem(grp.GroupName, grp.GroupId.ToString()));
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindCustomers(String.IsNullOrEmpty(e.Sort) ? "SubscriberName" : e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    protected void imgSearchButton_Click(object sender, EventArgs e)
    {

    }

    private void BindCustomers(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<NewsletterSubscriber> pagedCustomer = null;
        int totalCount = 0;

        int SubscriberGroupId;
        if (!Int32.TryParse(ddlToolbarGroups.SelectedItem.Value, out SubscriberGroupId))
            this._useSearch = false;

        if (this._useSearch)
        {
            pagedCustomer = NewsletterSubscriber.GetPagedSubscribtersByGroup(start+1, limit, sort, SubscriberGroupId);
            totalCount = NewsletterSubscriber.GetSubscriberCountByGroup(SubscriberGroupId);
            Session["_useSearch"] = null;
        }
        else
        {
            pagedCustomer = NewsletterSubscriber.GetPagedSubscribters(start+1, limit, sort);
            totalCount = NewsletterSubscriber.GetSubscriberCount();
        }

        Store1.DataSource = pagedCustomer;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateCustomer(int id, string name, string email)
    {
        NewsletterSubscriber subscriber = NewsletterSubscriber.Load(id);
        subscriber.SubscriberName = name;
        subscriber.Email = email;
        subscriber.Update();
        GridPanel1.Store.Primary.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteCustomers(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        NewsletterSubscriber.DeleteSubscribers(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchCustomer()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
